<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-form-item label="标题" prop="title">
				<el-input v-model="dataForm.title" placeholder="标题"></el-input>
			</el-form-item>
			<el-form-item label="分类id" prop="typeId">
				<el-tree-select
					v-model="dataForm.typeId"
					:data="typeList"
					value-key="id"
					check-strictly
					:render-after-expand="false"
					:props="{ value: 'id', label: 'name', children: 'children' }"
					style="width: 100%"
				/>
			</el-form-item>
			<el-form-item label="发布人id" prop="staffId">
				<el-input v-model="dataForm.staffId" placeholder="发布人id"></el-input>
			</el-form-item>
			<el-form-item label="附件" prop="fileUrl">
				<UploadImgView v-model="dataForm.fileUrl"></UploadImgView>
			</el-form-item>
			<el-form-item label="是否定时发送" prop="isTiming">
				<el-switch
					v-model="dataForm.isTiming"
					:active-value="1"
					:inactive-value="0"
					active-text="启用"
					inline-prompt
					inactive-text="禁用"
					placeholder=""
				/>
			</el-form-item>
			<el-form-item label="发布时间" prop="timingDate">
				<ElDatePicker type="datetime" v-model="dataForm.timingDate" placeholder="发布时间" />
			</el-form-item>
			<el-form-item label="公告内容" prop="contentStr">
				<WangEditor v-model="dataForm.contentStr" placeholder="请输入..."></WangEditor>
			</el-form-item>
			<el-form-item label="是否全员" prop="isAll" v-if="false">
				<el-input v-model="dataForm.isAll" placeholder="是否全员"></el-input>
			</el-form-item>
			<el-form-item label="是否置顶" prop="isTop">
				<el-switch
					v-model="dataForm.isTop"
					:active-value="1"
					:inactive-value="0"
					active-text="启用"
					inline-prompt
					inactive-text="禁用"
					placeholder=""
				/>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<style scoped>
.image {
	width: 100px;
	height: 50px;
}
</style>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import UploadImgView from '@/views/nb_module/image/upload-img-view.vue'
import { useAfficheApi, useAfficheSubmitApi } from '@/api/nb/news/affiche'
import WangEditor from '@/components/wang-editor/index.vue'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const typeList = ref()

const dataForm = reactive({
	id: '',

	title: '',
	typeId: '',
	staffId: '',
	fileUrl: '',
	isTiming: 1,
	timingDate: '',
	contentStr: '',
	isAll: '',
	isTop: 0
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getAffiche(id)
	}
}

const getAffiche = (id: number) => {
	useAfficheApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		if (dataForm.contentStr.length > 1000) {
			ElMessage.error({
				message:'<公告内容>内容过多，无法提交'
			})
		}

		useAfficheSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init,
	typeList
})
</script>
